package com.ysw.rpc.common.messages;

import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;

/**
 * @author YinShangwen
 * @since 2023/4/4 11:06
 */
@EqualsAndHashCode(callSuper = true)
@Data
@Accessors(chain = true)
public class Response extends Message {
    /**
     * 请求ID
     */
    private Long requestId;
    /**
     * 响应结果
     */
    private Object result;
    /**
     * 响应异常
     */
    private String error;

    public static Response fail(Request request, String error) {
        return init(request).setError(error);
    }

    public static Response ok(Request request, Object result) {
        return init(request).setResult(result);
    }

    public static Response init(Request request) {
        Response response = new Response();
        response.setRequestId(request.getId());
        response.setReferenceClassName(request.getReferenceClassName());
        response.setMethodName(request.getMethodName());
        return response;
    }
}
